package com.persistencia;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.entidades.xml.BaseObject;
import com.persistencia.util.RecordStrategy;

public class FileStringPersistence<T> implements Persistence<T> {

	private RecordStrategy recordData;
	// concurrent hash map is synchronized
	private final Map<String, T> dataMap = new ConcurrentHashMap<String, T>();

	@Override
	public Boolean create(T entidad) {
		BaseObject base = (BaseObject) entidad;
		dataMap.put(base.getId(), entidad);
		return true;
	}

	@Override
	public List<T> findAll() {
		List<T> list = new ArrayList<T>(dataMap.values());
		return list;
	}

	@Override
	public T findById(String id) {
		return dataMap.get(id);
	}

	@Override
	public Boolean update(T entidad) {
		return create(entidad);
	}

	@Override
	public Boolean delete(String id) {
		Object obj = dataMap.remove(id);
		if (obj == null) {
			return false;
		}
		return true;
	}

}
